/*-----------------------------------------------------------------------------------
Name: Sangyoon Park
Date: January 23 2023
This do file does : 
	Create variable labels and export codebook
-----------------------------------------------------------------------------------*/
clear all
set matsize 10000
set maxvar 10000
set more off


#delim ;

*** Load dataset;

import excel "${raw_path}/FARMER_COMBINETRADE_CLEAN.xlsx", firstrow;

*** Label dataset;

lab data "This file contains farmer reports on trade with intermediaries [BASE_G]";

*** Label variables;

lab var farmerid "unique id of farmer";
lab var strata "strata assigned to farmer";
lab var st_tgroup "treatment group assigned to farmer";
lab var follow"followup survey round (0 = baseline)";
lab var num"buyer number in survey";
lab var buyer_volume"percent of total volume sold to buyer";
lab var buyer_price"price sold to buyer";
lab var buyer_type"buyer's type";
lab var buyer_contract"have contract with buyer";
lab var buyer_exp"experience with buyer";
lab var buyer_market"product sold to country/market";
lab var buyer_harvest"buyer paid for harvesting cost";
lab var buyer_transport"buyer paid for transportation cost";
lab var buyer_know"where first meet buyer";


** Generate variables;

replace buyer_price = . if buyer_price == 0;
gen collector = (buyer_type == 1);
gen exporter = (buyer_type == 3);
gen retailer = (buyer_type == 4);
gen cont_formal = (buyer_contract == 1);
gen cont_verb = (buyer_contract == 2 | buyer_contract == 3) if followup == 1 | followup == 2;
gen cont_verbagree = (buyer_contract == 2) if followup == 1 | followup == 2;
gen progmeet = (buyer_know == 2) if followup == 1 | followup == 2;
replace buyer_contract = 4 if buyer_contract == 0;
replace buyer_contract = 4 if buyer_contract == 6;
replace buyer_contract = 2 if buyer_contract == 3;
replace buyer_contract = 3 if buyer_contract == 4;

label define marketlabel 1 "China" 2 "Asia" 3 "EU/US" 4 "Domestic";
label values buyer_market marketlabel;
tab buyer_market, gen(dum_market);

label define buyertypelabel 1 "Collector" 2 "Cooperative" 3 "Exporter" 4 "Retailer";
label define contractlabel 1 "Formal" 2 "Verbal" 3 "No contract";
label values buyer_type buyertypelabel;
label values buyer_contract contractlabel;


*** Label variables;

lab var collector "buyer type = collector";
lab var exporter "buyer type = exporter";
lab var retailer "buyer type = domestic retailer";
lab var cont_formal "have formal contract with buyer";
lab var cont_verb "have verbal agreement to sell product";
lab var cont_verbagree "have verbal agreement on price and quality";
lab var progmeet "First met buyer at program's group meeting";


*** Save dataset;

save "${data_path}/FARMER_COMBINETRADE_CLEAN", replace;

*** Create codebook for datafile;

log using "${out_path}/codebook_farmer_combinetrade_clean.log", replace;

codebook,compact;

log close;
*/

*use "${data_path}/FARMER_COMBINETRADE_CLEAN", replace;
*** Generate average price and revenue weighted by volume for each followup survey round;

merge m:1 farmerid using "${data_path}/FARMER_COMBINESURVEY_CLEAN";
keep if _merge == 3;
drop _merge;


gen perc_volume = buyer_volume/100;
gen weight_price = buyer_price*perc_volume;
gen weight_volume = volume_base*perc_volume if followup == 0;
replace weight_volume = volume_fu1*perc_volume if followup == 1;
replace weight_volume = volume_fu2*perc_volume if followup == 2;
gen weight_revenue = weight_price*volume_base if followup == 0;
replace weight_revenue = weight_price*volume_fu1 if followup == 1;
replace weight_revenue = weight_price*volume_fu2 if followup == 2;

bys farmerid followup: egen avgprice = sum(weight_price);
replace avgprice = . if avgprice == 0;

bys farmerid followup: egen sumrevenue = sum(weight_revenue);
replace sumrevenue = . if sumrevenue == 0;

gen weight_china = perc_volume*dum_market1;
gen weight_asia = perc_volume*dum_market2;
gen weight_eu = perc_volume*dum_market3;
gen weight_dom = perc_volume*dum_market4;

bys farmerid followup: egen avgmarket_china = sum(weight_china);
bys farmerid followup: egen avgmarket_asia = sum(weight_asia);
bys farmerid followup: egen avgmarket_eu = sum(weight_eu);
bys farmerid followup: egen avgmarket_dom = sum(weight_dom);
bys farmerid followup: gen avgmarket_lprice = avgmarket_china + avgmarket_dom;
bys farmerid followup: gen avgmarket_hprice = avgmarket_eu + avgmarket_asia;

bys farmerid followup: egen avgtrader_collector = mean(collector);
bys farmerid followup: egen avgtrader_exporter = mean(exporter);
bys farmerid followup: egen avgtrader_retailer = mean(retailer);


keep farmerid followup avgprice sumrevenue avgmarket_* avgtrader_*;
bys farmerid followup: keep if _n == 1;
reshape wide avgprice sumrevenue avgmarket_china avgmarket_asia avgmarket_eu avgmarket_dom avgmarket_lprice avgmarket_hprice avgtrader_collector avgtrader_exporter avgtrader_retailer, i(farmerid) j(followup);

rename avgprice0 tradeprice_base;
rename avgprice1 tradeprice_fu1;
rename avgprice2 tradeprice_fu2;

rename sumrevenue0 traderev_base;
rename sumrevenue1 traderev_fu1;
rename sumrevenue2 traderev_fu2;

rename avgmarket_china0 market_china_base;
rename avgmarket_asia0 market_asia_base;
rename avgmarket_eu0 market_eu_base;
rename avgmarket_dom0 market_dom_base;
rename avgmarket_lprice0 market_lprice_base;
rename avgmarket_hprice0 market_hprice_base;

rename avgmarket_china1 market_china_fu1;
rename avgmarket_asia1 market_asia_fu1;
rename avgmarket_eu1 market_eu_fu1;
rename avgmarket_dom1 market_dom_fu1;

rename avgmarket_china2 market_china_fu2;
rename avgmarket_asia2 market_asia_fu2;
rename avgmarket_eu2 market_eu_fu2;
rename avgmarket_dom2 market_dom_fu2;

rename avgtrader_collector0 trade_collector_base;
rename avgtrader_exporter0 trade_exporter_base;
rename avgtrader_retailer0 trade_retailer_base;

rename avgtrader_collector1 trade_collector_fu1;
rename avgtrader_exporter1 trade_exporter_fu1;
rename avgtrader_retailer1 trade_retailer_fu1;

rename avgtrader_collector2 trade_collector_fu2;
rename avgtrader_exporter2 trade_exporter_fu2;
rename avgtrader_retailer2 trade_retailer_fu2;


save "${data_path}/FARMER_COMBINETRADE_AVERAGE", replace;

clear;



























